'use strict'
// axios
import axios from 'axios';
import Mock from "mockjs";
import user from '../mock/user';

console.log("import user ", user)
let mocks = []

for (let index = 0; index < user.user.length; index++) {
    const element = user.user[index];
    mocks.push(element)
}
console.log("import mocks ", mocks)
import {
    Message
} from 'element-ui';
import qs from 'qs';

// require('promise.prototype.finally').shim();
//axios拦截器  request拦截
axios.interceptors.request.use(function(config) {
    // 在发送请求之前做些什么
    return config;

}, function(error) {
    // 对请求错误做些什么
    console.log('error --- error', error)
    return Promise.reject(error);
});
// 添加响应拦截器 response拦截
axios.interceptors.response.use(function(response) {

    return response;
}, function(error) {
    // 对响应错误做点什么

    if (error.request.status == '401') {

        if (document.getElementsByClassName('el-message').length === 0) {
            if (lang == "en") {
                Message.error("Please refresh the page or login again!")
            } else {
                Message.error("登录超时，请重新登录")
            }

        }

    } else if (!error.response) {

    } else {

    }
    // return Promise.reject(error);

});
//劫持axios相应的请求
Mock.mock('http://localhost:8080/industry', {
    'data': [{
        id: 1,
        iId: 0,
        industryName: "互联网/IT/电子/通信"
    }, {
        id: 2,
        iId: 0,
        industryName: "房地产"
    }, {
        id: 3,
        iId: 0,
        industryName: "金融业"
    }, {
        id: 4,
        iId: 0,
        industryName: "建筑业"
    }],
    'msg': "ok",
    'status': 200
});
console.log("mocks", mocks)
for (const i of mocks) {
    Mock.mock(new RegExp(i.url), i.type || 'get', i.response)
}

export {
    axios
};